﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace Baubixinh_client
{
    public partial class frmExcel : DevExpress.XtraEditors.XtraForm
    {
        public frmExcel()
        {
            InitializeComponent();
        }

        public bool fncExport(DataTable objData)
        {
            bool blnResult = false;
            try
            {
                if (objData == null) return true;

                for (int iCol = 0; iCol <= objData.Columns.Count - 1; iCol++)
                {
                    objExcel.Document.Worksheets[0].Cells[1, iCol].SetValue(objData.Columns[iCol].ColumnName);
                }

                int intExcelRow = 2;
                for (int iRow = 0; iRow <= objData.Rows.Count - 1; iRow++)
                {
                    for (int iCol = 0; iCol <= objData.Columns.Count - 1; iCol++)
                    {
                        objExcel.Document.Worksheets[0].Cells[intExcelRow, iCol].SetValue(objData.Rows[iRow][iCol]);
                    }
                    intExcelRow++;
                }

                blnResult = true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return blnResult;
        }

        public bool fncReportQty(DataTable objData)
        {
            bool blnResult = false;
            try
            {
                if (objData == null) return true;

                int intCol = 0;
                intCol++; objExcel.Document.Worksheets[0].Cells[1, intCol].SetValue("Tên sản phẩm");
                intCol++; objExcel.Document.Worksheets[0].Cells[1, intCol].SetValue("Mã SP");
                intCol++; objExcel.Document.Worksheets[0].Cells[1, intCol].SetValue("Tổng số lượng");
                intCol++; objExcel.Document.Worksheets[0].Cells[1, intCol].SetValue("Kho hàng");
                intCol++; objExcel.Document.Worksheets[0].Cells[1, intCol].SetValue("Màu sắc");
                intCol++; objExcel.Document.Worksheets[0].Cells[1, intCol].SetValue("Kích thước");
                intCol++; objExcel.Document.Worksheets[0].Cells[1, intCol].SetValue("Số lượng");

                int intProID = -1;
                string strProName = "";
                string strProEan = "";
                int intProQty = 0;
                string strKho = "";
                string strMau = "";
                string strSize = "";
                int intSoLuong = 0;

                int intPrevProID = -2;
                string strPrevKho = "a";
                string strPrevMau = "a";
                string strPrevSize = "a";

                int intExcelRow = 2;
                for (int iRow = 0; iRow <= objData.Rows.Count - 1; iRow++)
                {
                    if (intPrevProID != intProID)
                    {
                        intPrevProID = -2;
                        strPrevKho = "a";
                        strPrevMau = "a";
                        strPrevSize = "a";
                    }

                    //lay du lieu
                    intProID = clsCommon.fncCnvNullToInt(objData.Rows[iRow]["product_id"]);
                    strProName = clsCommon.fncCnvNullToString(objData.Rows[iRow]["pro_name"]);
                    strProEan = clsCommon.fncCnvNullToString(objData.Rows[iRow]["product_ean"]);
                    intProQty = clsCommon.fncCnvNullToInt(objData.Rows[iRow]["product_quantity"]);
                    strKho = clsCommon.fncCnvNullToString(objData.Rows[iRow]["ten_kho"]);
                    strMau = clsCommon.fncCnvNullToString(objData.Rows[iRow]["ten_mau"]);
                    strSize = clsCommon.fncCnvNullToString(objData.Rows[iRow]["ten_size"]);
                    intSoLuong = clsCommon.fncCnvNullToInt(objData.Rows[iRow]["soluong"]);

                    //fill du lieu
                    intCol = 0;

                    intCol++;
                    if (intPrevProID != intProID) objExcel.Document.Worksheets[0].Cells[intExcelRow, intCol].SetValue(strProName);

                    intCol++;
                    if (intPrevProID != intProID) objExcel.Document.Worksheets[0].Cells[intExcelRow, intCol].SetValue(strProEan);

                    intCol++;
                    if (intPrevProID != intProID) objExcel.Document.Worksheets[0].Cells[intExcelRow, intCol].SetValue(intProQty);

                    intCol++;
                    if (strPrevKho != strKho) objExcel.Document.Worksheets[0].Cells[intExcelRow, intCol].SetValue(strKho);

                    intCol++;
                    if (strPrevMau != strMau) objExcel.Document.Worksheets[0].Cells[intExcelRow, intCol].SetValue(strMau);

                    intCol++;
                    if (strPrevSize != strSize) objExcel.Document.Worksheets[0].Cells[intExcelRow, intCol].SetValue(strSize);

                    intCol++;
                    objExcel.Document.Worksheets[0].Cells[intExcelRow, intCol].SetValue(intSoLuong);

                    //luu du lieu cho lan sau
                    intPrevProID = intProID;
                    strPrevKho = strKho;
                    strPrevMau = strMau;
                    strPrevSize = strSize;

                    intExcelRow++;
                }

                blnResult = true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return blnResult;
        }
    }
}